package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿相关的接口")
@Slf4j
public class AddressBookController {
    @Autowired
    private  AddressBookService addressBookService;

    @ApiOperation("新增地址接口")
    @PostMapping
    public Result addAddress(@RequestBody  AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }


    @ApiOperation("查询当前登录用户的所有地址信息接口")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAllAddressByUserId(){
        List<AddressBook> bookList = addressBookService.getAllAddressByUserId();
        return Result.success(bookList);
    }


    @ApiOperation("查询查询默认地址接口")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        AddressBook defaultAddress = addressBookService.getDefaultAddress();
        return Result.success(defaultAddress);
    }

    @ApiOperation("根据id修改地址接口")
    @PutMapping
    public Result updateAddressById(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressById(addressBook);
        return Result.success();
    }


    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

}
